<?php
/**
 * mysql class
 *
 */
class MySqlBase
{
    private $conn;
    function __construct($basename = '', $commitmode = false)
    {
        $host = config::dbhost;
        $user = config::dbuser;
        $pass = config::dbpass;
        $basename = config::database;
        $this->conn = @mysql_connect($host, $user, $pass, true);
        if ($this->conn) {
            mysql_select_db($basename);
            mysql_query("set names 'utf-8'");
        } else {
            echo "Can't connect to database: ".$host."<br>";
        }
    }

    function __destruct() {
        @mysql_close($this->conn);
        $this->conn = null;
    }
    
    /**
	 * ִmysql query
	 *
	 * @param string $sql
	 * @return array
	 */
    function Query($sql) {
        $res = mysql_query($sql);
        if ($res) {
            $rowset = false;
            while ($row = mysql_fetch_assoc($res)) {
                $rowset[] = $row;
            }
            mysql_free_result($res);
            return $rowset;
        } else {
            echo mysql_errno($this->conn).": ".mysql_error($this->conn)."<br>";
            echo "SQL: ".$sql."<br>";
            return null;
        }
    }

    /**
	 * ִupdate or insert or delete
	 *
	 * @param string $sql
	 * @return bool
	 */

    function Execute($sql) {
        if (@mysql_query($sql)) {
            return true;
        } else {
            echo mysql_errno($this->conn).": ".mysql_error($this->conn)."<br>";
            echo "SQL: ".$sql."<br>";
            return false;
        }
    }

    /**
	 * ger insert id
	 *
	 * @return mixed
	 */
    function GetInsertID() {
        return mysql_insert_id();
    }

}

?>
